// pages/jielong/creatorder/creatorder.js
var app = getApp(); //引用app.js
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var jlpay = app.data.jlpay;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isblock: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;
    wx.request({
      url: jlpay +'op=getHistoryDeliverMessage',
      method:'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        openid:app.data.openid,
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
      },
      success:function(res){
        if(res.data.data.length>0){
          var length = res.data.data.length;
          that.setData({
            addname: res.data.data[length - 1].delivername,
            phone: res.data.data[length - 1].deliverphone,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    wx.getStorage({
      key: 'formid',
      success: function(res) {
        that.setData({
          formid:res.data
        })
      },
    })
    wx.getStorage({
      key: 'tzInfo',
      success: function(res) {
        console.log('设置成功'+res)
        that.setData({
          tzInfo: res.data
        })
      },
    })
    wx.getStorage({
      key: 'xiaoqu',
      success: function(res) {
        that.setData({
          xiaoqu: res.data
        })
      },
    })
    wx.getStorage({
      key: 'goodsArr',
      success: function(res) {
        console.log(res)
        var totalpayPrice = 0;
        var totalgoodPrice = 0;
        var goodlist = {};
        var selectgoods = [];
        for (var i = 0; i < res.data.length; i++) {
          totalpayPrice += Number(res.data[i].oprice) * Number(res.data[i].num),
            totalgoodPrice += Number(res.data[i].gprice) * Number(res.data[i].num),
          goodlist={
            gnum: res.data[i].num,
            unit: res.data[i].unit,
            optionid: res.data[i].optionid ? res.data[i].optionid:'',
            item: res.data[i].item ? res.data[i].item:'',
            weight: res.data[i].weight,
            mprice: res.data[i].mprice,
            oprice: res.data[i].oprice,
            gid: res.data[i].id,
          } 
          selectgoods.push(goodlist);
        }       
        that.setData({
          goods: res.data,
          activeid: res.data[0].activeid,
          totalpayPrice: totalpayPrice.toFixed(2),
          totalgoodPrice: totalgoodPrice.toFixed(2),
          selectgoods: selectgoods
        })
      },
    })
  },
  getname: function(e) {
   
    this.setData({
      addname: e.detail.value
    })
  },
  getphone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  createOrder: function() {
    var that=this;
    that.setData({
      isblock: true
    })
    if (!this.data.addname){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 4000
      })
      that.setData({
        isblock: false
      })
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(this.data.phone))) {
      wx.showToast({
        title: '手机号输入有误！',
        icon: 'none',
        duration: 4000
      })
      that.setData({
        isblock: false
      })
      return false;
    }
    if (wx.getStorageSync('xiaoqu')) {
      var areaid = wx.getStorageSync('xiaoqu').id
    } else {
      var areaid = wx.getStorageSync('tzInfo').area_id
    }
    if (!areaid){
      wx.showToast({
        title: '请选择小区',
        icon: 'none',
        duration: 4000
      })
      that.setData({
        isblock: false
      })
      return false;
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
    console.log(this.data.selectgoods)
    
    var param = {
      uniacid: app.data.GZHid,
      openid: wx.getStorageSync('openid'),
      unionId: app.data.unionId || wx.getStorageSync('unionId'),
      areaid: areaid,
      activeid: parseInt(this.data.activeid),
      delivername: this.data.addname,
      deliverphone: this.data.phone,
      goodsprice: this.data.totalgoodPrice,
      pay_price: this.data.totalpayPrice,
      goods: JSON.stringify(this.data.selectgoods),
      formid: JSON.stringify(this.data.formid)
    }
    console.log(typeof (JSON.stringify(this.data.selectgoods)))
    wx.request({
      url: jlpay +'op=createOrder',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: param,
      success: function(res) {
        wx.hideLoading();
        that.setData({
          isblock: false
        })
        console.log(res)       
        if(res.data.status==1){
        wx.redirectTo({
          url: '../confirmorder/confirmorder?orderno=' + res.data.data.orderno + '&oid=' + res.data.data.oid,
        })
        }else{
          wx.showToast({
            title: res.data.message,
          })
        }
      },
      fail:function(res){
        that.setData({
          isblock: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  selectarea: function() {
    wx.navigateTo({
      url: '../selectarea/selectarea',
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})